<?php
	/*
	Info: Handles all the ajax realtime automated calls (time Schedules)
	Author: Pritam Pebam
	Version: 1.0
	Contact: ptamzz@gmail.com; @ptamzz
	URL: http://12bubbles.com
	*/
	session_start();
	require_once('../inc/fnc.php');
	require_once('../inc/conn.php');
	
	//$storyId = $_SESSION['sid'];
	$storyId = $_POST['storyid'];
	
	if (isset($_SESSION['time'])) { $lastCall = $_SESSION['time']; } else { $lastCall = 0; }
	$_SESSION['time'] = time();
	$return = '';
	$storyMarkUp = '';

	$query = "SELECT * FROM storyline WHERE storyid = '{$storyId}' and storylineid > '{$_SESSION['stlastline']}'";
	
	$result = mysql_query($query,$connection);
	while($row = mysql_fetch_array($result)) {
		$storyMarkUp .=  getLine($storyId, $row['storylineid'], $row['storyline'], $row['crtdbyid']);
	}
	if ($storyMarkUp != ''){ $return['storyLine'] = $storyMarkUp; }
	$stline = getnewstorylineid($storyId);
	
	$query = "SELECT * FROM entryline WHERE storyid = '{$storyId}' and delflag = 'N' and storylineid = '{$stline}'";
	$result = mysql_query($query,$connection);
	
	$count = 'num-' . $storyId;
	if($result){
		if(isset($_SESSION[$count])){
			if(mysql_num_rows($result) == $_SESSION[$count] ){
				//No Actions required
			} else {
				//Return the entry lines
				$return['entryLine'] = returnEntryLines($result, $storyId, $connection);
			}
		} else {
			if(mysql_num_rows($result) > 0 ){
				//Return the entry lines
				$return['entryLine'] = returnEntryLines($result, $storyId, $connection);
			} else {
				//No Actions required
			}
		}
		$_SESSION[$count] = mysql_num_rows($result);
	}  // if($result){ ENDS
	
	
	//If vote changed
	$vote = 'vote-' . $storyId;
	$storyLineId = getnewstorylineid($storyId);
	$q = "SELECT count(entrylinevote.storyid) as votes FROM entrylinevote, entryline WHERE entryline.displayseq = entrylinevote.entrylineid and entryline.storyid = {$storyId} AND entryline.storylineid = {$storyLineId} AND entrylinevote.delflag = 'N' and entrylinevote.vote = 1";
	$result3 = mysql_query($q,$connection);
	while($row = mysql_fetch_array($result3)) {
		$totalVote = $row['votes'];
	}
	
	$q1 = "SELECT count(entrylinevote.storyid) as votes FROM entrylinevote, entryline WHERE entryline.displayseq = entrylinevote.entrylineid and entryline.storyid = {$storyId} AND entryline.storylineid = {$storyLineId} AND entrylinevote.delflag = 'N' and entrylinevote.vote = -1";
	$result4 = mysql_query($q1,$connection);
	while($row = mysql_fetch_array($result4)) {
		$totalVotedis = $row['votes'];
	}
		$totalVote = $totalVote - $totalVotedis;
	if(mysql_numrows($result3) > 0){
		if(isset($_SESSION[$vote])){
			if($_SESSION[$vote] == $totalVote){
				//No Actions required
			} else {
				//Return the entry lines
				$return['entryLine'] = returnEntryLines($result, $storyId, $connection);
			}
		}
		$_SESSION[$vote] = $totalVote;
	}  // if($result){ ENDS
	

	
	

	function returnEntryLines($result, $storyId, $connection) {
		include('../inc/conn.php');
		$i = 1;
		while($row = mysql_fetch_array($result)) {
			$upClass = "voteUp";
			$dwnClass = "voteDwn";
			$query1 = mysql_query("SELECT count(vote) as voteup from entrylinevote WHERE storyid = '{$storyId}' and entrylineid = '{$row['displayseq']}' and delflag = 'N' and vote = 1",$connection);
			while($row1 = mysql_fetch_array($query1)) { $vtup = $row1['voteup']; }
	
			$query1 = mysql_query("SELECT count(vote) as votedwn from entrylinevote WHERE storyid = '{$storyId}' and entrylineid = '{$row['displayseq']}' and delflag = 'N' and vote = -1",$connection);
			while($row1 = mysql_fetch_array($query1)) { $vtdwn = $row1['votedwn']; }
			
			$query1 = "SELECT vote from entrylinevote WHERE storyid = '{$storyId}' and entrylineid = {$row['displayseq']}  and delflag = 'N' and votebyuserid = {$_SESSION['userId']}";
			$result1 = mysql_query($query1);
			while($row1 = mysql_fetch_array($result1)) {
				$votetype = $row1['vote'];
				if($votetype == 1) {
					$upClass = "votedUp";
				} elseif($votetype == -1){
					$dwnClass = "votedDwn";
				}
			}
			
			$updateNode = "newline" . $i;
			$delNode = "line" . $i;
			$args = array(
						'count' => $i,
						'lineId' => $row['entrylineid'],
						'line' => $row['entryline'],
						'voteUp' => $vtup,
						'voteDwn' => $vtdwn,
						'updateNode' => $updateNode,
						'upClass' => $upClass,
						'dwnClass' => $dwnClass,
						'delNode' => $delNode,
						'deleter' => $_SESSION['deleter'],
						'user' => $row['crtdbyid'],
						'level' => 2,
						'uniqueid' => $row['displayseq'],
						'crtdbyid' => $row['crtdbyid'],
					);
			$entryMarkUp .=  getEntryLine($args);
			$i++;
		}
		
		if ($entryMarkUp != ''){
			$markUp = $entryMarkUp;
		}
		
		return $markUp;
	
	}

	echo json_encode($return);
?>